from django.shortcuts import render
from django.http import HttpRequest, HttpResponse, JsonResponse
from django.template import loader
from django.shortcuts import redirect
from django.views.decorators.clickjacking import xframe_options_deny
from django.views.decorators.clickjacking import xframe_options_sameorigin
from .util import checkPermission

import logging

from .models import Student, Teacher, Register, Class
from .util import strToSet, setToStr
import json

# Create your views here.
logger = logging.getLogger('django.request')

def login(request):
    # template = loader.get_template('aiAttendance/login.html')
    context = {}
    return render(request, 'aiAttendance/login.html', context)
    # return HttpResponse(template.render(context, request))

@xframe_options_sameorigin
def teacher(request):
    context = {}
    if not checkPermission(request):
        return redirect('../login')
    loginName = request.session.get('loginName')
    context['username'] = loginName
    return render(request, 'aiAttendance/teacher.html', context)

@xframe_options_sameorigin
def student(request):
    context = {}
    if not checkPermission(request):
        return redirect('../login')
    loginName = request.session.get('loginName')
    context['username'] = loginName
    return render(request, 'aiAttendance/student.html', context)

def createclass(request):
    context = {}
    if not checkPermission(request):
        return redirect('../login')
    context['username'] = request.session.get('loginName')
    tid = request.session.get('loginId')
    classes = Class.objects.values('classname').filter(teacher=int(tid))
    context['classes'] = list(classes)
    return render(request, 'aiAttendance/createclass.html', context)

def registers(request):
    context = {}
    if not checkPermission(request):
        return redirect('../login')
    context['username'] = request.session.get('loginName')
    tid = request.session.get('loginId')
    classes = Class.objects.values('classname').filter(teacher=int(tid))
    context['classes'] = list(classes)
    return render(request, 'aiAttendance/registers.html', context)

def checkregister(request):
    context = {}
    if not checkPermission(request):
        return redirect('../login')
    tid = request.session.get('loginId')
    classes = Class.objects.values('classname','id').filter(teacher=int(tid))
    context['classes'] = list(classes)
    return render(request, 'aiAttendance/checkregister.html', context)

def checkmyregister(request):
    context = {}
    if not checkPermission(request):
        return redirect('../login')
    tid = request.session.get('loginId')
    student = Student.objects.get(id=int(tid))
    classes = strToSet(student.classes)
    L = []
    for c in classes:
        s = Class.objects.get(id=int(c))
        cla = {
            "id": s.id,
            "classname": s.classname,
        }
        L.append(cla)
    context['student'] = student
    context['classes'] = L
    return render(request, 'aiAttendance/checkmyregister.html', context)

def doregister(request):
    context = {}
    if not checkPermission(request):
        return redirect('../login')
    tid = request.session.get('loginId')
    student = Student.objects.get(id=int(tid))
    classes = strToSet(student.classes)
    L = []
    for c in classes:
        s = Class.objects.get(id=int(c))
        cla = {
            "id": s.id,
            "classname": s.classname,
        }
        L.append(cla)
    context['student'] = student
    context['classes'] = L
    return render(request, 'aiAttendance/doregister.html', context)

def upload(request):
    context = {}
    if not checkPermission(request):
        return redirect('../login')
    tid = request.session.get('loginId')
    student = Student.objects.get(id=int(tid))
    context['student'] = student
    return render(request, 'aiAttendance/upload.html', context)
